import { defineStore } from 'pinia'
import { admApi } from '@/api/adm'
import type { Seal, Asset, MeetingRoom, Vehicle, Approval } from '../types/adm'

// 定义行政管理模块的状态接口
export interface AdmState {
  // 印章相关
  seals: Seal[]
  sealTotal: number
  currentSeal: Seal | null

  // 资产相关
  assets: Asset[]
  assetTotal: number
  currentAsset: Asset | null

  // 会议室相关
  meetingRooms: MeetingRoom[]
  meetingRoomTotal: number
  currentMeetingRoom: MeetingRoom | null

  // 车辆相关
  vehicles: Vehicle[]
  vehicleTotal: number
  currentVehicle: Vehicle | null

  // 审批相关
  approvals: Approval[]
  approvalTotal: number
  currentApproval: Approval | null
}

// 定义行政管理模块的store
export const useAdmStore = defineStore('adm', {
  state: (): AdmState => ({
    // 印章相关
    seals: [],
    sealTotal: 0,
    currentSeal: null,

    // 资产相关
    assets: [],
    assetTotal: 0,
    currentAsset: null,

    // 会议室相关
    meetingRooms: [],
    meetingRoomTotal: 0,
    currentMeetingRoom: null,

    // 车辆相关
    vehicles: [],
    vehicleTotal: 0,
    currentVehicle: null,

    // 审批相关
    approvals: [],
    approvalTotal: 0,
    currentApproval: null,
  }),

  actions: {
    // 印章管理相关actions
    // 获取印章列表
    async getSealListAction(params: any) {
      try {
        const response = await admApi.getSealList(params)
        if (response.code === 0) {
          this.seals = response.data
          this.sealTotal = response.data.length || 0
        }
        return response
      } catch (error) {
        throw error
      }
    },

    // 添加印章
    async addSealAction(data: any) {
      try {
        const response = await admApi.addSeal(data)
        return response
      } catch (error) {
        throw error
      }
    },

    // 编辑印章
    async editSealAction(data: any) {
      try {
        const response = await admApi.editSeal(data)
        return response
      } catch (error) {
        throw error
      }
    },

    // 删除印章
    async deleteSealAction(id: number) {
      try {
        const response = await admApi.deleteSeal(id)
        return response
      } catch (error) {
        throw error
      }
    },

    // 资产管理相关actions
    // 获取资产列表
    async getAssetListAction(params: any) {
      try {
        const response = await admApi.getAssetList(params)
        if (response.code === 0) {
          this.assets = response.data
          this.assetTotal = response.data.length || 0
        }
        return response
      } catch (error) {
        throw error
      }
    },

    // 添加资产
    async addAssetAction(data: any) {
      try {
        const response = await admApi.addAsset(data)
        return response
      } catch (error) {
        throw error
      }
    },

    // 编辑资产
    async editAssetAction(data: any) {
      try {
        const response = await admApi.editAsset(data)
        return response
      } catch (error) {
        throw error
      }
    },

    // 删除资产
    async deleteAssetAction(id: number) {
      try {
        const response = await admApi.deleteAsset(id)
        return response
      } catch (error) {
        throw error
      }
    },

    // 会议室管理相关actions
    // 获取会议室列表
    async getMeetingRoomListAction(params: any) {
      try {
        const response = await admApi.getMeetingRoomList(params)
        if (response.code === 0) {
          this.meetingRooms = response.data
          this.meetingRoomTotal = response.data.length || 0
        }
        return response
      } catch (error) {
        throw error
      }
    },

    // 添加会议室
    async addMeetingRoomAction(data: any) {
      try {
        const response = await admApi.addMeetingRoom(data)
        return response
      } catch (error) {
        throw error
      }
    },

    // 编辑会议室
    async editMeetingRoomAction(data: any) {
      try {
        const response = await admApi.editMeetingRoom(data)
        return response
      } catch (error) {
        throw error
      }
    },

    // 删除会议室
    async deleteMeetingRoomAction(id: number) {
      try {
        const response = await admApi.deleteMeetingRoom(id)
        return response
      } catch (error) {
        throw error
      }
    },

    // 车辆管理相关actions
    // 获取车辆列表
    async getVehicleListAction(params: any) {
      try {
        const response = await admApi.getVehicleList(params)
        if (response.code === 0) {
          this.vehicles = response.data
          this.vehicleTotal = response.data.length || 0
        }
        return response
      } catch (error) {
        throw error
      }
    },

    // 添加车辆
    async addVehicleAction(data: any) {
      try {
        const response = await admApi.addVehicle(data)
        return response
      } catch (error) {
        throw error
      }
    },

    // 编辑车辆
    async editVehicleAction(data: any) {
      try {
        const response = await admApi.editVehicle(data)
        return response
      } catch (error) {
        throw error
      }
    },

    // 删除车辆
    async deleteVehicleAction(id: number) {
      try {
        const response = await admApi.deleteVehicle(id)
        return response
      } catch (error) {
        throw error
      }
    },

    // 审批管理相关actions
    // 获取审批列表
    async getApprovalListAction(params: any) {
      try {
        const response = await admApi.getApprovalList(params)
        if (response.code === 0) {
          this.approvals = response.data
          this.approvalTotal = response.data.length || 0
        }
        return response
      } catch (error) {
        throw error
      }
    },

    // 添加审批
    async addApprovalAction(data: any) {
      try {
        const response = await admApi.addApproval(data)
        return response
      } catch (error) {
        throw error
      }
    },

    // 编辑审批
    async editApprovalAction(data: any) {
      try {
        const response = await admApi.editApproval(data)
        return response
      } catch (error) {
        throw error
      }
    },

    // 删除审批
    async deleteApprovalAction(id: number) {
      try {
        const response = await admApi.deleteApproval(id)
        return response
      } catch (error) {
        throw error
      }
    },

    // 审批操作
    async approveApprovalAction(data: { id: number; status: string; remark?: string }) {
      try {
        const response = await admApi.approveApproval(data)
        return response
      } catch (error) {
        throw error
      }
    },
  },
})
